//
//  YFWelcomeDeviceVC.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/15.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFWelcomeDeviceVC.h"
#import "CAAnimation+multipleAnimation.h"
#import "YFSearchController.h"

@interface YFWelcomeDeviceVC ()

@property (weak, nonatomic) IBOutlet UILabel *topTiplabel;
@property (weak, nonatomic) IBOutlet UIView *centerView;
@property (weak, nonatomic) IBOutlet UILabel *connectStateLabel;
@property (weak, nonatomic) IBOutlet UIImageView *blueToothIconImageView;
@property (weak, nonatomic) IBOutlet UIButton *skipButton;
/** 开启定时器检测设备是否连接成功 */
@property (nonatomic, strong) NSTimer *timer;

/** 是否在当前页面 */
@property (nonatomic, assign) BOOL isCurrentView;

@end

@implementation YFWelcomeDeviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topTiplabel.font = kOpenSansFont(OpenSansBold, 20);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openSearchDevice)];
    [self.centerView addGestureRecognizer:tap];
    [self regiesterNotification];
    [self beginOrCloseBlueToothImgAni:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.isCurrentView = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.isCurrentView = NO;
}

- (void)regiesterNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceConnectHandel) name:kLGPeripheralDidConnect object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceDidDisconnectHandle) name:kLGPeripheralDidDisconnect object:nil];
}
- (void)deviceConnectHandel {
    [self timer];
    [self beginOrCloseBlueToothImgAni:NO];
}

- (void)deviceDidDisconnectHandle {
    [self.timer timeInterval];
    self.timer = nil;
    [self beginOrCloseBlueToothImgAni:YES];
}

- (void)openSearchDevice {
    [YFSensorManager openSensorControllrWithVc:self isNecessaryOpenSensorVc:NO];
}

- (void)beginOrCloseBlueToothImgAni:(BOOL)open {
    if (open) {
        CABasicAnimation *blueAnimati = [CABasicAnimation opacityForever_Animation:1 formValue:1.0 toValue:0.2];
        [self.blueToothIconImageView.layer addAnimation:blueAnimati forKey:@"blueToothIconImageView"];
    }else {
         [self.blueToothIconImageView.layer removeAllAnimations];
    }
}



- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
//    CGFloat centerViewWH = (188 * kYFScreenWidth) / 340.0;
//    centerViewWH = centerViewWH > 190 ? centerViewWH: 190;
    [self.centerView layoutIfNeeded];
    CGFloat centerViewWH = self.centerView.bounds.size.width;
    [_centerView clipsCornerRadius:centerViewWH *0.5 borderWidth:6 borderColor:kYellowGreenColor];
    [_skipButton clipsCornerRadius:3];
    _centerView.backgroundColor = [UIColor clearColor];
}

- (IBAction)skipButtonClick:(id)sender {
//    if (self.skipOrNextStepBlock) {
//        self.skipOrNextStepBlock(NO);
//    }
    [self openSearchDevice];
}

- (void)checkDeviceConnectState {
    if (self.isCurrentView) {
        if ([YFCommunicationManager shareInstance].isPeripheralConnected) {
            if (self.skipOrNextStepBlock) {
                self.skipOrNextStepBlock(YES);
                [self.timer invalidate];
                self.timer = nil;
            }
        }
    }
}

- (NSTimer *)timer {
    if (_timer == nil) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkDeviceConnectState) userInfo:nil repeats:YES];
    }
    return _timer;
}


@end
